﻿using System.Collections.Generic;
using System.Linq;

namespace CodeContrib.Web
{
	public static class TaggableExtensions
	{
		public static decimal PercentageWeight<TTag>(this IEnumerable<Taggable<TTag>> taggables, int weight, int maxPercentage = 500) where TTag : class
		{
			if (weight == 0)
			{
				return 100;
			}

			var minWeight = taggables.Min(x => x.Weight);
			var maxWeight = taggables.Max(x => x.Weight);
			var distribution = maxWeight - minWeight;

			var percentage = 100 + (distribution * weight);

			if (percentage > maxPercentage)
			{
				return maxPercentage;
			}

			return percentage;
		}
	}
}